# 1. check pwsh version
echo $PSVersionTable.PSVersion  # 2.0.0
# 2. update powershell
New-Item -Force -Path $PROFILE -Type File
notepad.exe $PROFILE
# "C:\Users\w14512\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

# 🦜 : the testing running command
D:\RDTestClientData\common\tools\python38\python.exe -u -m pytest C:\Users\w14512\Downloads\hi\test_hi.py --custom-check="{'skip-steps':0}" `
   --testbed=C:\Users\w14512\Downloads\hi\hi.tbdx --script-log-path=C:\Users\w14512\Downloads\hi\log -s -W ignore::UserWarning -p no:cacheprovider --tb short set-executionpolicy remotesigned

# --------------------------------------------------
# user-name: ckf11246 pswd: 7Fb*a&T2 congjianer
ls to-deploy.zip
rm -rvf to-deploy
unzip to-deploy.zip

R=$(pwd)/dotnet-aaa
mkdir -vp $R
tar -xvf aspnetcore-runtime-8.0.18-linux-x64.tar.gz -C $R

# ln -s ../bk/wwwroot  ./wwwroot
DOTNET_ROOT=$R $R/dotnet ./bk/bk.dll --mock-rest \
--listening-address=http://0.0.0.0:5024                               \
--mock-infos=$(pwd)/c02-posts.tmp.json \
--mock-t=$(pwd)/T.json


# get ip
Get-NetIPAddress -AddressFamily IPv4 | Format-Table -Property IPAddress, InterfaceAlias

# --------------------------------------------------
# Step 1: Unzip and Prepare the Environment
cd ~/Music
# rm *.json
# rm -Recurse bk wwwroot
$z = "~/Desktop/to-supply.zip"
$f = "$PWD/05-to-gaul2"
rm -Recurse $f
Expand-Archive -LiteralPath $z -DestinationPath $PWD
ping 10.114.138.72
# 🦜 : ^ avoid double-bagging

# --------------------------------------------------
# Step 2: run with Mock stage I
cd $f
$R = "$PWD\..\dotnet-aaa"
$env:DOTNET_ROOT = $R
& "$R\dotnet" ".\bk\bk.dll" --mock-rest `
  --listening-address=http://0.0.0.0:5024 `
  --mock-infos="$PWD\c04-title.tmp.json" `
  --mock-t="$PWD\c03-chain.tmp.json"        #✅️

# --------------------------------------------------
# Step 3: run with Mock stage II
$R = "C:\Users\ckf11246\Music\dotnet-aaa"
$env:DOTNET_ROOT = $R
$n = "C:\Users\ckf11246\Music\db"
& "$R\dotnet" "./bk/bk.dll"  --mock-f4 `
  --n-parallel=3 `
  --back-grpc=http://10.114.138.72:50052 `
  --archive-folder=$n --listening-address=http://0.0.0.0:5024


# Step 3b: run with Mock grpc
$R = "C:\Users\ckf11246\Music\dotnet-aaa"
$env:DOTNET_ROOT = $R
$n = "C:\Users\ckf11246\Music\db"
mkdir $n
& "$R\dotnet" "./bk/bk.dll"  --mock-f4 `
  --n-parallel=3 `
  --archive-folder=$n --listening-address=http://0.0.0.0:5024

# --------------------------------------------------

# --------------------------------------------------
# ⚙️: update Front-end
cd ~\Music\05-to-gaul2
$n = "C:\Users\ckf11246\Desktop\to-supply01-FE.zip"
$f = "$PWD\wwwroot"
rm -Recurse $f
Expand-Archive -LiteralPath $z -DestinationPath $PWD


# --------------------------------------------------
# check log
# 1. read hi.jsonl into an array
$p = "c:\Users\ckf11246\Music\db\hi.jsonl"
# 2 get all objects
$o = @()
foreach($line in Get-Content $p) {
    $obj = $line | ConvertFrom-Json
    $o += $obj
}
$o | Select-Object -Property cid, creator
# 2.1 see the cid's
$o | Group-Object cid

# 3. filter the objects with "cid"  = "cid-4"
$o1 = $o | Where-Object { $_.cid -eq "cid-4" }

# Function to unescape Unicode and newlines
function Unescape($s) {
    $u = $s -replace '\\u([0-9a-fA-F]{4})', { [char][int]"0x$($_.Groups[1].Value)" }
    $n = $u -replace '\\n', "`n"
    return $n
}

# $s = "\u504c\u8bfe6"
$s = $o[1].logs
Unescape $s
